<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button @click="showaddcat" type="primary">添加分类</el-button>
        </el-col>
      </el-row>
<!--      表格-->
      <tree-table class="treetable" :data="catelist" :columns="columns"
                  :selection-type="false"
                  :expand-type="false"
                  :show-index="true"
                  :border="true">
      <template slot="isok" slot-scope="scope">
        <i style="color: lightblue" v-if="scope.row.cat_deleted === false" class="el-icon-success"></i>
        <i style="color: lightgray" v-else class="el-icon-error"></i>
      </template>
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" type="success" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level === 1" size="mini" type="warning">二级</el-tag>
          <el-tag v-else size="mini" type="danger">三级</el-tag>
        </template>
        <template slot="opt">
          <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
        </template>
      </tree-table>
<!--      分页-->
      <el-pagination class="fy"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <el-dialog
      @close="addcateclose"
      title="添加分类"
      :visible.sync="addcatdialogVisible"
      width="50%">
      <el-form :model="addcatform" :rules="addcatformrules" ref="addcatformref" label-width="100px">
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addcatform.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader class="xxx" expandTrigger="click"
            :options="parentcatelist"
            @change="parentcatechange"
            v-model="selectedkeys"
          :props="casaderprops" clearable change-on-select></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="addcatdialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addcate">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Cate',
  data () {
    return {
      queryinfo: {
        type: 3,
        pagenum: 1,
        pagesize: 7
      },
      catelist: [],
      total: 0,
      columns: [{
        label: '分类名称',
        prop: 'cat_name'
      }, {
        label: '是否有效',
        type: 'template',
        template: 'isok'
      }, {
        label: '排序',
        type: 'template',
        template: 'order'
      }, {
        label: '操作',
        type: 'template',
        template: 'opt'
      }],
      addcatdialogVisible: false,
      addcatform: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addcatformrules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      parentcatelist: [],
      casaderprops: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      selectedkeys: []
    }
  },
  created() {
    this.getcatelist()
  },
  methods: {
    async getcatelist () {
      const { data: res } = await this.$http.get('categories', { params: this.queryinfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品失败')
      }
      this.catelist = res.data.result
      this.total = res.data.total
      this.$message.success('获取成功')
    },
    handleSizeChange (newsize) {
      this.queryinfo.pagesize = newsize
      this.getcatelist()
    },
    handleCurrentChange (newpage) {
      this.queryinfo.pagenum = newpage
      this.getcatelist()
    },
    showaddcat () {
      this.getparencatelist()
      this.addcatdialogVisible = true
    },
    async getparencatelist () {
      const { data: res } = await this.$http.get('categories', {
        params: {
          type: 2
        }
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类数据失败')
      }
      this.parentcatelist = res.data
      this.$message.success('获取成功')
    },
    parentcatechange () {
      if (this.selectedkeys.length > 0) {
        this.addcatform.cat_pid = this.selectedkeys[this.selectedkeys.length - 1]
        this.addcatform.cat_level = this.selectedkeys.length
      } else {
        this.addcatform.cat_pid = 0
        this.addcatform.cat_level = 0
      }
    },
    addcate () {
      this.$refs.addcatformref.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.addcatform)
        if (res.meta.status !== 201) {
          return this.$message.error('添加失败')
        }
        this.$message.success('添加成功')
        this.getcatelist()
        this.addcatdialogVisible = false
      })
    },
    addcateclose() {
      this.$refs.addcatformref.resetFields()
      this.selectedkeys = []
      this.addcatform.cat_level = 0
      this.addcatform.cat_pid = 0
    }
  }
}
</script>

<style scoped>
 .treetable{
   margin-top: 20px;
 }
 .xxx{
   width: 100%;
 }
 .fy{
   margin-top: 20px;
 }
</style>
